<?php

/**
 * Controller/MotifController.php
 */

namespace RHX\Controller;

class MotifController extends RHXController {

    /**
     * Page d'accueil
     *
     */
    public function indexAction() {
	//
    }

    /**
     * Chargement du contenu
     *
     * @return array
     */
    public function loadIndexAction() {
	// Changement de layout
	$this->layout('layout/ajax');
	// Table Motif
	$tableMotif	 = new \RHX\Model\Table\MotifTable($this->getAdapter());
	// Liste des Motifs
	$motifs		 = $tableMotif->select(function(\Zend\Db\Sql\Select $select) {
		    $select->order('libelle ASC');
		});

	return array(
	    'motifs' => $motifs,
	);
    }

    /**
     * Ajouter un motif
     *
     * @return \Zend\View\Model\JsonModel
     */
    public function addAction() {
	// Changement de layout
	$this->layout('layout/ajax');
	// post
	$post		 = $this->getRequest()
		->getPost();
	// Nouveau
	$motif		 = new \RHX\Model\Object\Motif($post);
	// Table Motif
	$tableMotif	 = new \RHX\Model\Table\MotifTable($this->getAdapter());
	// Sauvegarde
	$id		 = $tableMotif->save($motif);
	$motif->id	 = $id;

	return new \Zend\View\Model\JsonModel(array(
	    'done'	 => ($id != 0) ? true : false,
	    'motif'	 => $motif,
	));
    }

    /**
     * Editer un motif
     *
     * @return \Zend\View\Model\JsonModel
     */
    public function editAction() {
	// changement de layout
	$this->layout('layout/ajax');
	// post
	$post	 = $this->getRequest()
		->getPost();
	// variables
	$pk	 = $post->pk;
	$name	 = $post->name;
	$value	 = addslashes($post->value);
	$sql	 = "UPDATE rh_motif_depart SET $name = '$value' WHERE id = $pk";
	$result	 = $this->getAdapter()
		->query($sql)
		->execute();

	return new \Zend\View\Model\JsonModel(array(
	    'done'	 => (!$result) ? false : true,
	    'post'	 => $post,
	));
    }

    /**
     * Effacer un motif
     *
     * @return \Zend\View\Model\JsonModel
     */
    public function eraseAction() {
	// changement de layout
	$this->layout('layout/ajax');
	// post
	$post		 = $this->getRequest()
		->getPost();
	// table motif
	$tableMotif	 = new \RHX\Model\Table\MotifTable($this->getAdapter());
	$nb		 = $tableMotif->erase($post->id);

	return new \Zend\View\Model\JsonModel(array(
	    'done'	 => ($nb > 0) ? true : false,
	    'nb'	 => $nb,
	    'post'	 => $post,
	));
    }

}

?>
